﻿package com.module.systemConfig.manager;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.systemConfig.dao.SystemConfigDao;
import com.module.systemConfig.vo.SystemConfig;

public class SystemConfigManager extends
		GenericHibernateManager<SystemConfig, Long> {

	private SystemConfigManager() {
	}

	private static SystemConfigManager manager;

	public synchronized static SystemConfigManager getInstance() {
		if (manager == null)
			manager = new SystemConfigManager();
		return manager;
	}

	private SystemConfigDao dao = SystemConfigDao.getInstance();

	/**
	 * 查询列表
	 * 
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public Page list(Page pageObj, SystemConfig pu) throws Exception {
		Session session = this.getSession();
		Page page = new Page();
		try {
			page = dao.getList(session, pu, pageObj.getStart(),
					pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}

	/**
	 * 新增/修改
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> add(SystemConfig obj) throws Exception {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			if (null != obj.getId() && 0 != obj.getId()) {// 修改
				obj.setStatus("U");
				flag = dao.update(session, obj);

			} else {
				obj.setStatus("C");
				flag = dao.save(session, obj);
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		map = optHandle(flag);
		return map;
	}

	/**
	 * 删除任务
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> delete(Long id) throws Exception {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		Map<String, Object> map = new HashMap<String, Object>();
		boolean flag = false;
		try {
			SystemConfig obj = dao.get(session, id);
			obj.setStatus("D");
			flag = dao.update(session, obj);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;

		} finally {
			this.closeSession();
		}
		map = optHandle(flag);
		return map;

	}

	/**
	 * 根据id查找
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public SystemConfig findById(Long id) throws Exception {
		Session session = this.getSession();
		SystemConfig vo = new SystemConfig();
		try {
			vo = dao.get(session, id);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return vo;
	}

	/**
	 * 根据属性名获取属性值
	 * 
	 * @param keyName
	 * @return
	 */
	public SystemConfig getConfigValue(String keyName) throws Exception {
		String configValue = "";
		int dataType = 0;
		Session session = this.getSession();
		SystemConfig vo = new SystemConfig();
		try {
			vo = dao.getUniqueByProperty(session, "configKey", keyName);
			configValue = vo.getConfigValue();
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return vo;
	}

	/**
	 * 操作处理返回
	 * 
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue) {
		String status = null;
		status = (isTrue == true ? "操作成功！" : "操作失败！");
		return optHandle(isTrue, status);
	}

	/**
	 * 操作处理返回
	 * 
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue, String message) {
		Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);
		statusMap.put("msg", message);
		return statusMap;
	}

	@Override
	public GenericHibernateDao<SystemConfig, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}

	public SystemConfig queryByKey(String configKey) throws Exception {
		String name = configKey;
		String hql = "from SystemConfig as obj where obj.configKey='"
				+ name + "' and status <> 'D'";
		Session session = this.getSession();
		try {
			return dao.findUniqueObjectBySQL(session, hql);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
	}
}
